Skip to content

第10回Go勉強会の課題@luccafort #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luccafort
Copy link

課題

https://mf.esa.io/posts/132264

  • Sleep SortでWaitGroupを使って確実にGoroutineの実行完了を待てる様にしてください。

sleep_sortディレクトリの実装をerrgourp#Groupとerrgroup#Waitを用いてGoroutineの終了を同期するよう変更
実行したgoroutine内でエラーが発生したときのハンドリングを実装
Copy link
Author

@luccafort luccafort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

気になったところや考えてたことなどをコメント。

)

// mutex
var mu sync.Mutex
var sortedNums []int
Copy link
Author

@luccafort luccafort Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[コメント]

そういえば今回グローバル領域に変数を定義したわけだけど個人的にはあまりグローバル領域に変数を定義したくないわけで、じゃあどうすればいいのか?ってところまで突き詰められてなかった。

defer mu.Unlock()
sortedNums = append(sortedNums, n)
}(num) // (num)が無名関数の引数
eg.Go(SleepLessThan100(num))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[コメント]

この eg.Go でラップするのが少し気になった。
JSのように async / awaitな構文が欲しくなったけどそれだと多分隠蔽しすぎてるという思想なのかなと思った。
JSのように動的言語であればそっちのほうが嬉しいけど型のある世界でそれを隠蔽してしまうと副作用も隠蔽することになるので明示的に宣言させてそうだなって思いました。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あとJSと違って複数のGoroutineの終了を同期したい、みたいな要件も違ってるだろうなというのがある。

}

// SleepLessThan100 using SleepSort and return error when variable more than 100.
func SleepLessThan100(n int) func() error {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[コメント]

戻り値の型指定で func() error を返すというのがどうにも違和感がある。
ただこれは慣れの問題かなと思ってる。

return errors.New("n must be less than 100")
}
// n秒スリープする
time.Sleep(time.Duration(n) * time.Second)
Copy link
Author

@luccafort luccafort Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[コメント]

Sleep Sortというソートの概念知らなかった。
知らないアルゴリズムだったのでめちゃくちゃ難しいことしてるのか!?と思ったら逆だったのウケる

@luccafort luccafort marked this pull request as ready for review April 29, 2020 14:22

func init() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

雰囲気で特殊な組み込み関数をオーバーライドしていてmain関数の実行前に初期化処理が行えるんだろうな、くらいに考えてた。
https://qiita.com/tenntenn/items/7c70e3451ac783999b4f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant